<template>
	<view class="page">
		<view class="selectBox">
			<view class="selectItem" v-for="(item,index) in selectClassArr" :key="index" @click="handleSelectItem(index)">
				<u-radio-group v-model="item.checked">
					<u-radio :name="true" :label="item.courseName"></u-radio>
				</u-radio-group>
				<view class="selectRow">
					<text class="moneyText red">￥{{item.cost}}</text>
					<text class="tipBtn">{{item.choseType==1 ?'选交':'必交'}}</text>
					<text>份数:({{item.cntCopies ? item.cntCopies : '不限'}})</text>
				</view>
			</view>
		</view>
		<view class="row">
			<view class="label">
				{{'合计'}}:
			</view>
			<view class="text red" style="font-weight: 600;">
				{{totalMoney}}
			</view>
		</view>
		
		<view class="textTitle">
			{{language.topup.label4}}
		</view>
		<view class="payType">
			<u-icon name="weixin-fill" color="#10d269" size="30"></u-icon>
			<text>
				{{language.topup.label5}}
			</text>
			<u-icon name="checkmark" color="#2979ff" size="28"></u-icon>
		</view>
		<view class="btn" @click="handleTopup">
			<text>{{'支付'}}</text>
		</view>
		
	</view>
</template>

<script>
	import { csSubjectQueryListApi,paymentCouresApi} from '@/common/api/commonApi/bugClass.js'
	export default {
		data() {
			return {
				selectClassArr:[
					// {
					// 	checked: true,
					// 	label : '美术绘画',
					// 	tip: '选交',
					// 	num: '不限',
					// 	money: 150
					// },
				],
				
				selectMoney: '',
			
				deductionAmount: '',
				serviceCharge :'',
				cardSn : '',
				selectInfoAccount : uni.getStorageSync('selectInfoAccount'),
				themeId :'',
				subjectIdList : [],
				payBool: false,
				
			}
		},
		computed: {
			totalMoney(){
				let num = 0
				let subjectIdList = []
				this.selectClassArr.forEach(item=>{
					if(item.checked){
						num+=item.cost
						subjectIdList.push(item.id)
					} 
				})
				this.subjectIdList = subjectIdList
				return num
			}
		},
		methods: {
			handleSelectItem(index){
				console.log(this.selectClassArr[index]);
				let item = this.selectClassArr[index]
				item.checked = !item.checked
				this.$set(this.selectClassArr,index,item)
			},
			getList(){
				return csSubjectQueryListApi({
					themeId : this.themeId
				}).then(res=>{
					this.selectClassArr = []	
					res.result.forEach(item=>{
						item.checked = false
					})
					this.selectClassArr = res.result
				})
			},
			handleTopup(){
				if(!this.totalMoney) return this.setMessage('请先选择课程')
				
				let data = {
					money : this.totalMoney,
					themeId : this.themeId,
					subjectIdList : this.subjectIdList,
					tradeType: 'JSAPI'
				}
				if(this.payBool) return
				this.payBool = true
				paymentCouresApi(data).then(res=>{
						uni.requestPayment({
							provider: 'wxpay',
							nonceStr :  res.result.nonce_str,
							package : `prepay_id=${res.result.prepay_id}`,
							paySign : res.result.sign,
							signType : 'MD5',
							timeStamp: String(res.result.timeStamp),
							success:() =>{
									this.setMessage('支付成功')
									setTimeout(()=>{
										uni.reLaunch({
											url:'/pages/index/index'
										})
									},1000)
							},
							fail: (err) => {
								console.log(err);
								this.payBool = false
								this.setMessage('支付取消或失败')
							},
							// complete:()=> {
							// 	this.payBool = false
							// }
						})
				
				}).catch(err=>{
					console.log(err);
					this.payBool = false
				})
			}
		},
		onLoad({id}) {
			this.themeId = id
			this.getList()
		
		},
		onShow() {
			
		},
	}
</script>

<style lang="scss" scoped>
.page{
	background-color: #fff;
	height: 100vh;
	.text{
		color: #333;
	}
	.row{
		display: flex;
		align-items: center;
		height: 90rpx;
		border-bottom: 2rpx solid #f1f1f1;
		&:nth-child(1){
			border-top: 2rpx solid #f1f1f1;
		}
		.label{
			margin: 0 30rpx;
		}
	
	}
	.red{
		color: #fe415c;
	}
	.money{
		height: 100rpx;
		@include flexCenter;
		font-size: 36rpx;
		border-bottom: 2rpx solid #f1f1f1;
		text{
			font-size: 28rpx;
			color: #333;
			margin-left: 5rpx;
		}
	}
	.textTitle{
		margin-top: 40rpx;
		font-size: 30rpx;
		padding-left: 50rpx;
		color: #000;
	}
	.payType{
		display: flex;
		align-items: center;
		height: 110rpx;
		padding: 0 50rpx;
		width: 100%;
		image{
			width: 50rpx;
			height: 60rpx;
		}
		text{
			color: #000;
			flex: 1;
			padding-left: 20rpx;
			white-space: nowrap;
			font-size: 30rpx;
		}
	}
	.btn{
		margin: 20rpx auto 0;
		background: #2d8cf6;
		@include flexCenter;
		width: 90vw;
		height: 92rpx;
		border-radius: 10rpx;
		text{
			color: #fff;
		}
	}
	
	.selectBox{
		min-height: 700rpx;
		padding-top: 30rpx;
		.selectItem{
			background-color: #fff;
			margin: 0 30rpx 30rpx;
			border-radius: 8rpx;
			padding: 10rpx;
			box-shadow: 0px 0px 20rpx 0px rgba(102,102,102,0.35);
			padding: 20rpx;
			.selectRow{
				display: flex;
				margin-top: 20rpx;
				justify-content: space-evenly;
				color: #c2c2c2;
				text{
					// flex: 1;
					display: flex;
					align-items: center;
				}
				.moneyText{
					// flex: 1;
					font-size: 38rpx;
				}
			}
			
		}
	}

	.tipBtn{
		color: #0072e5;
		padding:  5rpx 10rpx;
		border: 2rpx solid #0072e5;
		border-radius: 10rpx;
	}
}
</style>
